package com.swipemaster;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

class PainterThread extends Thread
{
	private SurfaceHolder mSurfaceHolder;
	private Panel mPanel;
	private boolean mRun = false;

	public PainterThread(SurfaceHolder surfaceHolder, Panel panel)
	{
		mSurfaceHolder = surfaceHolder;
		mPanel = panel;
	}

	public void setRunning(boolean run)
	{
		mRun = run;
	}

	@Override
	public void run()
	{
		Canvas canvas;
		while (mRun)
		{
			canvas = null;
			try
			{
				canvas = mSurfaceHolder.lockCanvas(null);
				synchronized (mSurfaceHolder)
				{
					mPanel.updatePhysics();
					mPanel.onDraw(canvas);
				}
			}
			finally
			{
				// Do this in a finally so that if an exception is thrown during the above,
				// we don't leave the Surface in an inconsistent state.
				if (canvas != null)
				{
					mSurfaceHolder.unlockCanvasAndPost(canvas);
				}
			}
		}
	}

	public SurfaceHolder getSurfaceHolder()
	{
		return mSurfaceHolder;
	}
}